package com.picture.service.impl;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.picture.constant.MessageConstant;
import com.picture.mapper.PictureMapper;
import com.picture.pojo.entity.Picture;
import com.picture.pojo.entity.Picturecategory;
import com.picture.result.Result;
import com.picture.service.PicturecategoryService;
import com.picture.mapper.PictureCategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 悦神
* @description 针对表【picturecategory】的数据库操作Service实现
* @createDate 2024-03-01 16:50:07
*/
@Service
public class PicturecategoryServiceImpl extends ServiceImpl<PictureCategoryMapper, Picturecategory>
    implements PicturecategoryService {


    @Autowired
    PictureCategoryMapper picturecategoryMapper;

    @Autowired
    PictureMapper pictureMapper;
    @Override
    public void delete(int id) {

        LambdaQueryWrapper<Picture> p = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Picture> eq = p.eq(Picture::getPictureCategoryId, id);
        List<Picture> list = pictureMapper.selectList(eq);
        if (list!=null&&list.size()>0){
            throw new RuntimeException(MessageConstant.PICTURE_CATEGORY);
        }
        picturecategoryMapper.deleteById(id);
    }

    @Override
    public void add(String name) {
        LambdaQueryWrapper<Picturecategory> p = new LambdaQueryWrapper<>();
        p.eq(Picturecategory::getName,name);
        List<Picturecategory> picturecategories = picturecategoryMapper.selectList(p);
        if (picturecategories!=null&&picturecategories.size()>0)
            throw new RuntimeException(MessageConstant.PICTURE_CATEGORY_EXIST);
        Picturecategory picturecategory = new Picturecategory();
        picturecategory.setName(name);
        picturecategoryMapper.insert(picturecategory);
    }

    @Override
    public Result<List<Picturecategory>> categorylist() {
        List<Picturecategory> picturecategories = picturecategoryMapper.selectList(new QueryWrapper<>());
        return Result.success(picturecategories);
    }
}




